<?php


namespace App\Http\Controllers\Supplier;

use App\Http\Controllers\Controller;
use App\Models\AuthGroup;
use App\Models\NewAdmin;
use App\Models\ShopUser;
use App\Services\AuthService;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class BaseController extends BaseVerifyController
{
    protected $users;
    protected $shopUser;
    protected $userGroup;
    //过滤验证 [目录\controller@action]
    protected $filterAction = [
//        'Supplier\LoginController@login',
    ];

    public function __construct(Request $request)
    {
        parent::__construct($request);
        if(AuthService::checkFilter(request()->route()->action['controller'],$this->filterAction)){
            $userToken = $request->header("SupplierAuth");
            // $user="7e06c1a5e5a97b5d246b6eeae65af34b";
            $user = json_decode(Redis::get($userToken),true);
            $user = NewAdmin::where(['token'=>$userToken,'type'=>AuthService::MODEL_MERCHANT])->first();
            $user = obToArray($user);
            // dd($user);
            if($user){
                if($user['is_delete'] == CommonService::IS_DELETE_YES){
                    //删除
                    echo json_encode(['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '账号不存在', 'data' => []],JSON_UNESCAPED_UNICODE);exit;
                }
                if($user['status'] == CommonService::USER_STATUS_NOT){
                    //冻结
                    echo json_encode(['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '账号已冻结', 'data' => []],JSON_UNESCAPED_UNICODE);exit;
                }
                $authGroup = AuthGroup::where(['id'=>$user['role_id'],'is_delete'=>CommonService::IS_DELETE_NOT,'status'=>CommonService::USER_STATUS_YES])->first();
                $authGroup = $authGroup?$authGroup->toArray():[];


                if($authGroup){
                    if(!AuthService::checkClassAction($authGroup['rules'])){
                        //无权限
                        echo json_encode(['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '无权限', 'data' => []],JSON_UNESCAPED_UNICODE);exit;
                    }
                    $this->userGroup = $authGroup;
                    $this->users = $user;
                    $shopUser = ShopUser::where(['member_id'=>$user['user_id'],'is_delete'=>ShopUser::SHOP_USER_NOT_DELETE])->first();
                    $shopUser = obToArray($shopUser);
                    if($shopUser){
                        $this->shopUser = $shopUser;
                        return true;
                    }
                }
            }
            echo json_encode(['status' => CommonService::HTTP_CODE_NOT_LOGIN, 'msg' => '请登录', 'data' => []],JSON_UNESCAPED_UNICODE);exit;
        }
    }



}
